// pages/earlyEducation/index.js
const util = require("../../utils/util");
const api = require("../../api/api");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: '',
    imgPath: "/images",
    videoCoverImg:'http://qnstatic.lianhequnmei.com/images/20221219/iconvideo.png',
    videoPlayIcon:'http://qnstatic.lianhequnmei.com/images/20221219/iconshop.png',
    isShow:true,
    //列表数据
    listData: [],

    //标题
    videoTitle: "第1节-宝宝的教育",
    //视频路径
    // videoPath: 'https://vd4.bdstatic.com/mda-mkretpr4g0fei13s/sc/cae_h264_nowatermark/1637936044962665206/mda-mkretpr4g0fei13s.mp4?v_from_s=hkapp-haokan-nanjing&auth_key=1669176195-0-0-26cc9d261d673022ccca5483b6bcea6d&bcevod_channel=searchbox_feed&pd=1&cd=0&pt=3&logid=1995741786&vid=6161144966451257578&abtest=&klogid=1995741786',
    videoPath: '',
    //视频描述
    videoDescription: '这是一款依托于世界权威婴幼儿早期教育标准–The Early Years Foundation Stage（EYFS）体系理论研发而成的，专门针对0-3岁婴幼儿的线上课程。',

    //标题夹活动夹标题,第一级
    titleActive: '',
    //第二级活动
    l2_activeName: '',
  },
  options: {
    styleIsolation: "shared"
   },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    //图片基础路径
    this.setData({
      imgPath: util.getImgPath()
    })

    this.setData({
      titleActive: '',
      videoTitle: "",
      l2_activeName: '',
      videoPath:'',
    })

    this.getData();
    // 获取本地存储中的数据 
    // const videoList = wx.getStorageSync('videoList');
    // if(!videoList){
    //     //取得视频列表
    //     this.getData();
    // }else{
    //   this.setData({
    //     listData: videoList,
    //     l2_activeName: videoList[0].sub_list[0].title
    //   });
    // }
   
  },
  bindplay(){
         wx.setNavigationBarTitle({
            title: this.data.listData[0].sub_list[0].sub_list[0].title,
          })
    const that=this;
    let id = this.data.listData[0].sub_list[0].sub_list[0].id;
    api.video.getVideoUrl({
      video_id: id,
      success(resV) {
        // console.log("getVideoUrl,resV", resV);
        let videoPath = resV.data.data;
        //保存列表数据 
        that.setData({
          videoPath: videoPath,
          isShow:false
        });
        // console.log("that.data", that.data)
      }
    })

  },
  /**
   * 取得视频列表
   */
  getData() {
    const that = this;
    // wx.showLoading({
    //   title: '加载中',
    // })
    api.video.getList({
      success(res) {
        //wx.hideLoading();
         console.log("getList,res", res);
        let rows1 = res.data.data;
        // console.log("rows1", rows1);
        wx.setStorageSync('videoList', rows1)
        //保存列表数据 
        that.setData({
          listData: rows1,
          l2_activeName: rows1[0].sub_list[0].title
        });

        // rows=that.data.listData;
        for (const row1 of rows1) {
          if (that.data.titleActive == "") {
            that.setData({
              titleActive: row1.title
            })
          }

          for (const row2 of row1.sub_list) {
            //第二级活动
            if (that.data.l2_activeName == "") {
              that.setData({
                l2_activeName: row2.title,
              })
            }

            for (const row3 of row2.sub_list) {


              if (that.data.videoTitle == "") {
                //第三级标题
                that.setData({
                  videoTitle: row3.title,
                  videoDescription: '',
                })

                // api.video.getVideoUrl({
                //   video_id: row3.id,
                //   success(resV) {
                //     console.log("getVideoUrl,resV", resV);
                //     row3.videoPath = resV.data.data;

                //     //保存列表数据 
                //     that.setData({
                //       listData: rows1,
                //     });
                //     console.log("that.data", that.data)
                //   }
                // })
              }

            }

          }
        }


      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  onTabbarChange(event) {
   //  console.log("onTabbarChange,event", event)
    this.setData({
      active: event.detail,
    })
   
 
  },
  /**
   * 第一级标题改变事件
   * @param {*} e 
   */
  onTitleChange(e) {
   // console.log("onTitleChange,e", e)
    this.setData({
      titleActive: e.detail,
     l2_activeName: this.data.listData[e.detail.index].sub_list[0].title
    })
  },
  onL2Change(e) {
   // console.log("onL2Change,e", e)
    this.setData({
      l2_activeName: e.detail,
    })
  },
  /**
   * 播放视频
   * @param {*} e 
   */
  play(e) {
   // console.log("play,e", e.currentTarget.dataset.title)
     wx.setNavigationBarTitle({
            title: e.currentTarget.dataset.title,
          })
    const that=this;
    let id = e.currentTarget.dataset.id;

    api.video.getVideoUrl({
      video_id: id,
      success(resV) {
        // console.log("getVideoUrl,resV", resV);
        let videoPath = resV.data.data;

        //保存列表数据 
        that.setData({
          videoPath: videoPath,
          isShow:false
        });
        // console.log("that.data", that.data)
      }
    })
  }
})